<?php

//VideoFlow - Joomla Multimedia System for Facebook//

/**
* @ Version 1.1.3
* @ Copyright (C) 2008 - 2010 Kirungi Fred Fideri at http://www.fidsoft.com
* @ VideoFlow is free software
* @ Visit http://www.fidsoft.com for support
* @ Kirungi Fred Fideri and Fidsoft accept no responsibility arising from use of this software 
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
**/ 

// No direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport( 'joomla.application.component.view');
 
class VideoflowViewVideoflow extends JView
{

  function __construct()
    {
      parent::__construct();
               
      global $vparams;  
      
      $vtask = JRequest::getCmd ('task');
      
      $tmpl = JRequest::getWord ('tmpl');
      
      JHTML::_('behavior.modal', 'a.modal-vflow');
      
      $this->loadVflowModules();
      
      if ($vtask == 'myvids' || $vtask == 'subscribe' || $vtask == 'visit' || $vtask == 'mysubs') $this->setOwner();
      
      if ($vparams->lightbox && ($vparams->lightboxsys=='multibox')) $this->loadLightbox();
      
      if ($tmpl == 'component') $this->loadCstyle();
     
      if ($vparams->facebook){
          $this-> loadFbconn();    
      }    
    }


    
    function display()
    {
        global $vparams;
        $model =& $this->getModel();
        $vlist = $model->getData ();
        $clist = $model->getCategories();
        if(!empty ($vlist)) $vlist = $model->updateData($vlist);
        $pagination = $model-> getPagination ();
        $menu = $model->getMenu();
        $this->assignRef ('media', $vlist);
        $this->assignRef ('menu', $menu); 
        $this->assignRef ( 'pagination', $pagination);   
        $this->assignRef ('cats', $clist);   
        parent::display();
    }

   
   function displayMedia($id)
   {
        global $vparams;
        $lo = JRequest::getCmd('layout');
        $xparams = &JComponentHelper::getParams( 'com_videoflow' );
        $tabtitlelength = (int) $xparams->get('tabtitlelength',10);
        $xlimit = (int) $xparams->get('limit', $vparams->limit);
        $model = $this->getModel();
        $media = $model->getMedia($id);
        if ($lo == 'playerview') {
        $related = $model->getRelated($media, $xlimit);
        $pagination = $model->getPagination();
        } else {
        $related = $model->getRelated($media);
        } 
        $byuser = $model -> getByuser ($media->userid, $id);
        if (!empty($tabtitlelength)) {
        if (!empty($related)) $related = $model->xterWrap($related, array("title"=>$tabtitlelength));
        if (!empty($byuser)) $byuser = $model->xterWrap($byuser, array("title"=>$tabtitlelength));
        }
        if (!empty($vparams->commentsys)) $comments = $model->getComments($media);
        $menu = $model->getMenu();
        if ($vparams->ratings) $vrating = $model->createRating ($id);
        $add = JRequest::getInt('add', 1);
        if ($add == 0) $add = 'remove'; else $add = 'add';
        $tools = $model->getTools($media, $add);
        $this->assignRef('media', $media);
        $this->assignRef ('tools', $tools);
        $this->assignRef('tabone', $related); 
        $this->assignRef ('tab1count', JRequest::getInt('vcount', ''));
        $this->assignRef ('tabtwo', $byuser);
        $this->assignRef ('tab2count', JRequest::getInt('ucount', ''));
        $this->assignRef ('menu', $menu); 
        $this->assignRef ('comments', $comments);
        $this->assignRef ('rating', $vrating);
        if (!empty($pagination)) $this->assignRef('pagination', $pagination);
        $this->sendHeaders($media);   
        parent::display(); 
   }
  
   function displayMyvids($myid)
   {
        global $vparams;
        $task = JRequest::getCmd ('task');
        $model = $this->getModel();
        $id = $uid = $fid = JRequest::getInt('id', null);
        $lo = JRequest::getCmd('layout');
        if ($lo == 'playerview' || $lo == 'ajaxlist'){
        $this->pvDisplayMyvids($myid);
        return;
        }
        if ($id) $media = $model->getMedia($id);
        $tab = JRequest::getWord ('tab');
        if ($tab == 'one') {
        $fid = null;
        } else if ($tab == 'two') {
        $uid = null;
        } else {
        $uid = $fid = null;
        }
        if ($lo == 'playerview' || $lo == 'ajaxlist') {
        $uid = $id;
        }
        $uploads = $model->getByuser($myid, $uid);
        $favourites = $model->getFavourites($myid, $fid);
        $xparams = &JComponentHelper::getParams( 'com_videoflow' );
        $tabtitlelength = (int) $xparams->get('tabtitlelength',10);
        if (!empty($tabtitlelength)) {
        if (!empty($uploads)) $uploads = $model->xterWrap($uploads, array("title"=>$tabtitlelength));
        if (!empty($favourites)) $favourites = $model->xterWrap($favourites, array("title"=>$tabtitlelength));
        }

        $fcount = JRequest::getInt('fcount', 0);
        $ucount = JRequest::getInt('ucount', 0);
        $add = JRequest::getInt('add', 1);
        if ($add) $add = 'add'; else $add = 'remove';
        if (empty($media) && $lo != 'playerview' && $lo != 'ajaxlist') {
            if (!empty ($uploads)){
            $media = $uploads [0];
            unset ($uploads[0]);
            $ucount = $ucount - 1;
            } 
            elseif (!empty($favourites)){
            $media = $favourites[0];
            unset ($favourites[0]);
            $fcount = $fcount - 1;
            $add = 'remove';
            } else {
            $media = $model->getMedia(null);
            if (empty($vparams->help)) {
            $help = JRoute::_('http://videoflow.fidsoft.com/index.php?option=com_content&tmpl=component&id=58#playlist');
            } else {
            $help = JRoute::_($vparams->help);
            }
            JError::raiseWarning(400, JText::_('You have not yet uploaded or added any media to your playlist!').' '.
            '<a href="'.$help.'" class="modal-vflow" rel="{handler: \'iframe\', size: {x: \'600\', y: \'480\'}}">'.
            JText::_('Click here for help.').'</a>');
            }
                if ($media){
                $media->usrname = $model->getUsername ($media->userid);
                $media->shortname = $model->runTool('xterWrap', $media->usrname, $vparams->shortname);
                $media->embedcode = $model->_buildCode($media);
                $media->shorttitle = $model->runTool('xterWrap', $media->title, $vparams->shorttitle);
                $media->catname = $model->getCatName ($media->cat);
                if (!empty($media->tags)) $media->autotags = $model->processTags ($media->tags); else $media->autotags = '';
                } 
        }
            
        
        
        if (!empty($media)) {
            if ($vparams->ratings) $vrating = $model ->createRating ($media->id);
            if (!empty($vparams->commentsys)) $comments = $model->getComments($media);
            $tools = $model->getTools ($media, $add);
            $this->assignRef('media', $media);
            $this->assignRef ('comments', $comments);
            $this->assignRef ('rating', $vrating);
            $this->assignRef ('tools', $tools);
            $this->sendHeaders($media);
        }
        
        if ($task == 'myvids') {
        $juser_id = $myid; 
        } else {
        $juser = JRequest::getVar('juser');
          if (empty($juser)) $juser = & JFactory::getUser();
        $juser_id = $juser->id;
        }
        $subaction = $model->setSubAction($juser_id);
        if ($subaction) $this->assignRef('subaction', $subaction);
        $menu = $model->getMenu();
        $subcount = $model->countSubscribers ($myid);
        if (!$subcount) $subcount = 0;
        $this->assignRef ('menu', $menu); 
        $this->assignRef ('favcount', JRequest::getInt('f_count', 0));
        $this->assignRef ('uploadcount', JRequest::getInt('u_count', 0));
        $this->assignRef ('visitcount', $model->setVisitors($myid));
        $this->assignRef ('subcount', $subcount);
        if ($lo == 'playerview' || $lo == 'ajaxlist'){
        $pagination = $model-> getPagination ();
        $this->assignRef('pagination', $pagination);
        if (!empty($uploads)) {
        $uploads = $model->updateData($uploads);
        $this->assignRef ('vlist', $uploads);
        } else if (!empty($favourites)){
        $favourites = $model->updateData($favourites);
        $this->assignRef ('vlist', $favourites);
        }
        } else {
        $this->assignRef('tabone', $uploads); 
        $this->assignRef ('tabtwo', $favourites);
        $this->assignRef ('tab1count', $ucount);
        $this->assignRef ('tab2count', $fcount);
        }
        parent::display(); 
   }

    function pvDisplayMyvids($myid)
    {
        global $vparams;
        $task = JRequest::getCmd ('task');
        $list = JRequest::getWord ('list'); 
        $model = $this->getModel();
        $id = JRequest::getInt('id');
        $add = JRequest::getInt('add', 1);
        if (empty($add)) $add = 'remove'; else $add = 'add';
        if (empty($list) || $list == 'ups') {
        if ($id) $media = $model->getMedia($id);
        $vlist = $model->getByuser($myid, $id);
        $mylist = 'uploads';
        $favcount = $model->countFavs($myid);
        }
        if (empty($vlist) && (empty($list) || $list == 'favs')) {
        $vlist = $model->getFavourites ($myid, $id);            
        $mylist = 'favourites';
        $upcount = $model->countUploads($myid);
        }
        if (empty($media) && empty($vlist)) {
            $media = $model->getMedia(null);
            if (empty($vparams->help)) {
            $help = JRoute::_('http://videoflow.fidsoft.com/index.php?option=com_content&tmpl=component&id=58#playlist');
            } else {
            $help = JRoute::_($vparams->help);
            }
            if ($list == 'favs') {
            $errmes = 'You have not yet added any media to your playlist!';
            if ($task == 'visit') $errmes = 'This channel has no favourite media selected.';
            } else if ($list = 'ups') {
            $errmes = 'You have not yet uploaded any media!';
            if ($task == 'visit') $errmes = 'No media has been uploaded to this channel yet.';
            } else {
            $errmes = 'You have not yet uploaded or added any media to your playlist!';
            if ($task == 'visit') $errmes = 'This channel has no favourite or uploaded media yet.';
            }
            if ($task == 'visit') {
            JError::raiseWarning(400, JText::_($errmes));
            } else {
            JError::raiseWarning(400, JText::_($errmes).' '.
            '<a href="'.$help.'" class="modal-vflow" rel="{handler: \'iframe\', size: {x: \'600\', y: \'480\'}}">'.
            JText::_('Click here for help.').'</a>');
            }              
        }        
        $xparams = &JComponentHelper::getParams( 'com_videoflow' );
        $ajaxmode = (int) $xparams->get('ajaxmode');
        if (!empty($media)) {
            if($mylist == 'favourites') $add = 'remove'; else $add = 'add'; 
            if ($vparams->ratings) $vrating = $model ->createRating ($media->id);
            if (!empty($vparams->commentsys)) $comments = $model->getComments($media);
            $tools = $model->getTools ($media, $add);
            $this->assignRef('media', $media);
            $this->assignRef ('comments', $comments);
            $this->assignRef ('rating', $vrating);
            $this->assignRef ('tools', $tools);
            if (empty($ajaxmode)) {
            $this->sendHeaders($media);
            }
        }
        
        if ($task == 'myvids') {
        $juser_id = $myid; 
        } else {
        $juser = JRequest::getVar('juser');
          if (empty($juser)) $juser = & JFactory::getUser();
        $juser_id = $juser->id;
        }
        $subaction = $model->setSubAction($juser_id);
        if ($subaction) $this->assignRef('subaction', $subaction);
        $menu = $model->getMenu();
        $subcount = $model->countSubscribers ($myid);
        if (!$subcount) $subcount = 0;
        if (empty($favcount)) $favcount = JRequest::getInt('f_count', 0);
        if (empty($upcount)) $upcount = JRequest::getInt('u_count', 0);
        $this->assignRef ('menu', $menu); 
        $this->assignRef ('mylist', $mylist);
        $this->assignRef ('myid', $myid);
        $this->assignRef ('favcount', $favcount);
        $this->assignRef ('uploadcount', $upcount);
        $this->assignRef ('visitcount', $model->setVisitors($myid));
        $this->assignRef ('subcount', $subcount);
        if (!empty($vlist)) {
        $vlist = $model->updateData($vlist);
        $this->assignRef ('vlist', $vlist);
        $pagination = $model-> getPagination ();
        $this->assignRef('pagination', $pagination);
        }
        $this->setOwner();
        parent::display(); 
  }


  
  function displayUserfavs()
  {
    $usrid = JRequest::getInt ('usrid');
    $model = $this->getModel();
    $menu = $model->getMenu();
    $favourites = $model->getFavourites ($usrid);
    $clist = $model->getCategories();
    if (!empty($favourites)) {
            $favourites = $model->updateData($favourites);
            $pagination = $model-> getPagination ();
            $this->assignRef ('media', $favourites);
            $this->assignRef ( 'pagination', $pagination); 
            }     
    $this->assignRef ('cats', $clist);
    $this->assignRef ('menu', $menu);
    parent::display();
  }
  
  function displayMysubs($myid)
  {
    global $vparams;
    $id = JRequest::getInt('id');
    $pid = JRequest::getInt('pid');
    $model = $this->getModel();
    $menu = $model->getMenu();
    $subs = $model->getMysubs ($myid);
    if (!empty($subs)) {
    $subs = $model->getCname ($subs);
      if (!empty($pid)) {
      $vids = $model->getByuser ($pid);
      if (empty($vids)) $vids = $model->getFavourites($pid);
      } else {
      $vids = $model->findMedia ($subs, 'joomla_id', 'uploads');   
      $utext = JText::_('Uploads');
          if (empty($vids)) {
          $vids = $model->findMedia($subs, 'joomla_id', 'favourites');
          $utext = JText::_('Favourites');
          }
      }
      if (empty($vids)) $utext = JText::_('This channel has no media');
    } else {
    JError::raiseWarning(400, JText::_('You have not yet subscribed to any channel!'));
    }
    if (!empty($vids)) {
        $xparams = &JComponentHelper::getParams( 'com_videoflow' );
        $tabtitlelength = (int) $xparams->get('tabtitlelength',10);
        $vids = $model->xterWrap($vids, array("title"=>$tabtitlelength));
    }
    if (!empty($id)) {
    $media = $model->getMedia($id);
    } else if (!empty($vids)) {
    $id = $vids ['0']->id;
    unset ($vids [0]);
    $media = $model->getMedia($id);
    }
    
    if (!empty($media)) {
        $tools = $model->getTools($media, 'add');
        if ($vparams->ratings) $vrating = $model ->createRating ($media->id);
        if (!empty($vparams->commentsys)) $comments = $model->getComments($media);
        $this->assignRef('media', $media);
        $this->assignRef ('comments', $comments);
        $this->assignRef ('rating', $vrating);
        $this->assignRef ('tools', $tools);
        $this->sendHeaders($media);
        }
    $ucount = JRequest::getInt ('ucount');
    $fcount = JRequest::getInt ('fcount');
    $subcount = $model->countSubscribers ($myid);
    $favcount = $model->countFavs($myid);
    $uploadcount = $model->countUploads ($myid);

    $this->assignRef ('tabone', $subs);
    $this->assignRef ('tabtwo', $vids);
    $this->assignRef ('tab2count', $ucount);
    $this->assignRef ('tab1count', $fcount);
    $this->assignRef ('menu', $menu);
    $this->assignRef ('utext', $utext);
    $this->assignRef ('favcount', $favcount);
    $this->assignRef ('uploadcount', $uploadcount);
    $this->assignRef ('visitcount', $model->setVisitors($myid));
    $this->assignRef ('subcount', $subcount);
    parent::display();
  }

  
   
   function displaySearch()
   {
        global $vparams;
        $model = $this->getModel();
        $menu = $model->getMenu();
        $clist = $model->getCategories();
        $this->assignRef ('menu', $menu);
        $this->assignRef ('cats', $clist);
        $vs = JRequest::getString ('vs');
        if (!empty($vs)){
            $vlist = $model->doSearch();
            if (!empty($vlist)) {
            $vlist = $model->updateData($vlist);
            $pagination = $model-> getPagination ();
            $this->assignRef ( 'pagination', $pagination); 
            $this->assignRef ('media', $vlist);
            }
        } 
        parent::display();
   } 
   
   function displaySearchPlay()
   {
        global $vparams;
        $id = JRequest::getInt('id');
        $related = null;
        $xparams = &JComponentHelper::getParams( 'com_videoflow' );
        $xlimit = (int) $xparams->get('limit', $vparams->limit);
        $model = $this->getModel();
        $menu = $model->getMenu();
        if (!empty($id)) {
        $media = $model->getMedia($id);
        $related = $model->getRelated($media, $xlimit);
        if (!empty($vparams->commentsys)) $comments = $model->getComments($media);
        if ($vparams->ratings) $vrating = $model->createRating ($id);
        $add = JRequest::getInt('add', 1);
        if ($add == 0) $add = 'remove'; else $add = 'add';
        $tools = $model->getTools($media, $add);
        $this->assignRef('media', $media);
        $this->assignRef ('tools', $tools);
        $this->assignRef ('comments', $comments);
        $this->assignRef ('rating', $vrating);
        $this->sendHeaders($media);  
        $sw = $media->tags;
        if (empty($sw)) $sw = $media->title;
        JRequest::setVar('searchword', $sw); 
        } else {
        $related = $model->doSearch();
        }
        if (!empty($related)) {
        $related = $model->updateData($related);
        }
        $vcount = JRequest::getInt('vcount', 0);
        $model->_total = $vcount;
        $pagination = $model->getPagination();
        $this->assignRef('vlist', $related); 
        $this->assignRef ('menu', $menu); 
        if (!empty($pagination)) $this->assignRef('pagination', $pagination);
        parent::display(); 
   }

   
   function displayAdd()
   {
    include_once(JPATH_COMPONENT_SITE.DS.'html'.DS.'videoflow_html.php');
    videoflowHTML::addForm();
   }
   
   
   function displayEmbedForm($data)
   {
    $bselect = array(
    JHTML::_('select.option', '0', JText::_('No') ),
    JHTML::_('select.option', '1', JText::_('Yes') )
    );
    
    $lselect = array (
    JHTML::_('select.option', 'music', JText::_('Music') ),
    JHTML::_('select.option', 'video', JText::_('Video') ),
    JHTML::_('select.option', 'games', JText::_('Games') )
    );
    
    $this->assignRef ('data', $data);
    $this->assignRef ('bselect', $bselect);
    $this->assignRef ('lselect', $lselect);
    parent::display();
   }
   
   function displayUploadForm($data)
   {
    include_once(JPATH_COMPONENT_SITE.DS.'html'.DS.'videoflow_html.php');
    videoflowHTML::uploadForm($data);
   }
   
   function displayEditForm ($data)
   {
    $model = $this->getModel();
    $bselect = array(
    JHTML::_('select.option', '0', JText::_('No') ),
    JHTML::_('select.option', '1', JText::_('Yes') )
    );
    $data->bselect = $bselect;
    $data->catlist = $model->getCategories();
    if (empty($data->selcat)) $data->selcat = $data->cat;
    include_once(JPATH_COMPONENT_SITE.DS.'html'.DS.'videoflow_html.php');
    videoflowHTML::editForm($data);
  }
   
   
   function displayChannel()
   {
    $model =& $this->getModel();
    parent::display();   
   }
   
   function displayCshare()
   {
      $model = $this->getModel();
      $data = $model->fbInvite(); 
      $this->assignRef ('data', $data);
      parent::display();   
   }
   
   function displayCategories()
   {
      $model = $this->getModel();
      $cats = $model ->getCatList(); 
      $xparams = &JComponentHelper::getParams( 'com_videoflow' );
      $shorttitle = (int) $xparams->get('shorttitle');
      $shortdetails = (int) $xparams->get('shortdetails');
      if (!empty($shorttitle) || !empty($shortdetails)) {
        if (!empty($shorttitle)) $fields['name'] = $shorttitle;
        if (!empty($shortdetails)) $fields['desc'] = $shortdetails; 
        if (!empty($cats)) $cats = $model->xterWrap($cats, $fields);
      }      

      $menu = $model ->getMenu();
      $pagination = $model-> getPagination ();
      $this->assignRef ('data', $cats);
      $this->assignRef ('menu', $menu);
      $this->assignRef ('pagination', $pagination);
      parent::display();
   }
   
   
   function displayByCat ($vlist) 
   {
      $model = $this->getModel();
      $cat = JRequest::getInt ('cat');
      if (empty ($cat)) $catname = JText::_('VF_CAT_NONE'); else $catname = $model->getCatName($cat);
      $vlist = $model->updateData($vlist);
      $clist = $model->getCategories();
      $vcount = count($vlist);
      $pagination = $model-> getPagination ();
      $this->assignRef ( 'pagination', $pagination); 
      $menu = $model ->getMenu();
      $this->assignRef ('cats', $clist);
      $this->assignRef ('media', $vlist);
      $this->assignRef ('menu', $menu);
      $this->assignRef ('catname', $catname);
      parent::display();
   }
   
   function displayCats($vlist)
   {
      global $vparams;
      $model = $this->getModel();
      $cat = JRequest::getInt ('cat');
      if (empty ($cat)) $catname = JText::_('VF_CAT_NONE'); else $catname = $model->getCatName($cat);
      $id = JRequest::getInt('id');
      $media = '';
      if ($id) $media = $model->getMedia($id);
      $xparams = &JComponentHelper::getParams( 'com_videoflow' );
        $tabtitlelength = (int) $xparams->get('tabtitlelength', 10);
        if (!empty($tabtitlelength)) {
        if (!empty($vlist)) $vlist = $model->xterWrap($vlist, array("title"=>$tabtitlelength));
        }      
      $vcount = count($vlist);
      if (!$media && (!empty($vlist))) {
      $media = $vlist [0];
      unset ($vlist[0]);
      $vcount = $vcount - 1;
      $media = $model ->getMedia ($media->id);
      } 
      if ($media) {
            if ($vparams->ratings) $vrating = $model ->createRating ($media->id);
            if (!empty($vparams->commentsys)) $comments = $model->getComments($media);
            $tools = $model->getTools ($media);
            $this->assignRef('media', $media);
            $this->assignRef ('comments', $comments);
            $this->assignRef ('rating', $vrating);
            $this->assignRef ('tools', $tools);
            $this->sendHeaders($media);
        }

      $cats = $model ->getCatList($vparams->sidebarlimit); 
      $tabdetailslength = (int) $xparams->get('tabdetailslength', 40);
        if (!empty($tabtitlelength) || !empty($tabdetailslength)) {
        if (!empty($tabtitlelength)) $fields2['name'] = $tabtitlelength;
        if (!empty($tabdetailslength)) $fields2['desc'] = $tabdetailslength; 
        if (!empty($cats)) $cats = $model->xterWrap($cats, $fields2);
        }
      $menu = $model ->getMenu();
      if (count($vlist) > $vparams->sidebarlimit) $vlist = array_slice($vlist, 0, $vparams->sidebarlimit); 
      $this->assignRef ('tabone', $vlist);
      $this->assignRef ('tabtwo', $cats);
      $this->assignRef ('tab1count', $vcount);
      $this->assignRef ('tab2count', JRequest::getInt('count', 0));
      $this->assignRef ('menu', $menu);
      $this->assignRef ('catname', $catname);
      parent::display();
   }
   
   function displayEmail($id)
   {
        global $vparams;
        $model = $this->getModel();
        $eshare = JRequest::getCmd('task');
        if ($eshare == 'eshare'){
        $media = $model->getChannel($id);
        } else {
        $elink = JRequest::getVar('link', '', 'get', 'base64');
        $media = $model ->getMedia($id); 
        $media->elink = $elink;
        }
        include_once(JPATH_COMPONENT_SITE.DS.'html'.DS.'videoflow_html.php');
        videoflowHTML::emailForm($media);
   } 
   
   function displayStatus()
   {
        parent::display();
   } 
   

   function displayLogin ()
   {
    global $vparams;
    $model = $this->getModel();
    $menu = $model->getMenu();
    $this->assignRef ('menu', $menu);
    parent::display();
   }

  function displayLogout ()
  {
    $model = $this->getModel();
    $menu = $model->getMenu();
    $user = & JFactory::getUser();
    if (!$user->guest){
    $name = $user->name;
    $this->assignRef ('name', $name);
    }
    $this->assignRef ('menu', $menu);
    parent::display();
  }
     
   function sendHeaders($media)
   {
    global $vparams;   
    $fmt = JRequest::getCmd('format');
    if ($fmt == 'raw') return;
    if ($media->server == 'local' && stristr($media->file, 'http') === FALSE) {
        include_once (JPATH_COMPONENT_SITE.DS.'helpers'.DS.'videoflow_file_manager.php'); 
        $subdir = VideoflowFileManager::getFileInfo ($media->type);
        $media->file = JURI::root().$vparams->mediadir.'/'.$subdir['dir'].'/'.$media->file;
    }
      if (!empty($media->pixlink)) {
         if ($media->server == 'local' && stripos($media->pixlink, 'http') === FALSE) {  
         $media->pixlink = JURI::root().$vparams->mediadir.'/_thumbs/'.$media->pixlink;
         }
       }
      if (empty($media->pixlink)) $media->pixlink = JURI::root().'components/com_videoflow/players/preview.jpg';
         
         if($vparams->player == 'neolao'){
         $vfcon = mt_rand();
         $vfplayer = JURI::root().'components/com_videoflow/players/neolao.swf';
         $mtype = 'flv';
         if ($media->type == 'mp3') $mtype = 'mp3';
         $vlink = $vfplayer.'?'.$mtype.'='.$media->file.'&v='.$vfcon.'&width='.$vparams->metapwidth.'&height='.$vparams->metapheight.'&autoplay=1&top1='.$vparams->logo.'|-15|15&startimage='.$media->pixlink;
         } else if ($vparams->player == 'JW'){
         $vfplayer = JURI::root().'components/com_videoflow/players/player.swf';
         $vlink = $vfplayer.'?file='.$media->file.'&width='.$vparams->metapwidth.'&height='.$vparams->metapheight.'&autostart=true&logo='.$vparams->logo.'&skin='.$vparams->skin.'&image='.$media->pixlink;
         } else {
         $vfplayer = JURI::root().'components/com_videoflow/players/NonverBlaster.swf'; 
         $autoval = 'true';
         if ($media->type == 'mp3') $autoval = 'false'; 
         $vlink = $vfplayer.'?mediaURL='.$media->file.'&width='.$vparams->metapwidth.'&height='.$vparams->metapheight.'&autoPlay=true&indentImageURL='.$vparams->logo.'&teaserURL='.$media->pixlink.'&showTimecode=true&crop=false';
         } 
         if (!empty($media->metaplay) && is_array($media->metaplay)) {
         if (stripos($media->metaplay['player'], '?') === false) $q = '?'; else $q = '&';
         $vlink = $media->metaplay['player'].$q.ltrim($media->metaplay['flashvars'], '&').'&width='.$vparams->metapwidth.'&height='.$vparams->metapheight;
         }
        $doc =& JFactory::getDocument();
        $doc->setGenerator('VideoFlow Multimedia System V.1.1.3f');
        $doc->setTitle($media->title);
        $doc->setDescription ($media->details);
        $doc->setMetaData('keywords', $media->tags);
        $doc->setMetaData ('video_width', $vparams->metapwidth);
        $doc->setMetaData ('video_height', $vparams->metapheight);
        $doc->setMetaData ('video_type', 'application/x-shockwave-flash');
        $doc->addCustomTag( '<link rel="image_src" href="'.$media->pixlink.'" />');
        $doc->addCustomTag( '<link rel="video_src" href="'.$vlink.'" />');
   }
   
   function loadLightbox()
   {
   global $vparams;
      $doc = &JFactory::getDocument();
      $css = JURI::root().'components/com_videoflow/views/videoflow/tmpl/multibox/multibox.css';
      $doc->addStyleSheet( $css, 'text/css', null, array() );
      if ($vparams->mootools12){
      $mootools=JURI::root().'components/com_videoflow/views/videoflow/tmpl/multibox/mootools.js';
      $doc->addScript( $mootools );
      }
      if (!$vparams->vmode) {
      $this->mboffset = 0;
      } else {
      $this->mboffset = 1;
      }
      $overlay = JURI::root().'components/com_videoflow/views/videoflow/tmpl/multibox/overlay.js';
      $doc->addScript( $overlay );
      $multibox = JURI::root().'components/com_videoflow/views/videoflow/tmpl/multibox/multibox.js';
      $doc->addScript( $multibox );
   }
   
   function loadCstyle()
   {
      $doc = &JFactory::getDocument();
      $css = JURI::root().'templates/system/css/system.css';
      $doc->addStyleSheet( $css, 'text/css', null, array() );
   }
   
   function loadFbconn()
   {
   global $vparams;
      $fmt = JRequest::getCmd('format');
      if ($fmt == 'raw') return; 
      $task = JRequest::getCmd('task');
      $fbuser = JRequest::getInt('fbuser');
      if ($vparams->facebook){
      $doc = & JFactory::getDocument();
      $fbconn = 'http://static.new.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php';
      $doc->addScript( $fbconn );
      if ($task != 'uploadmedia' && $task != 'upload') {
      echo '<body onload="initFB();">';
      }     
      if (!empty($fbuser)) {
        $perms = JRequest::getVar('perms');
        if (empty($perms[0]['publish_stream'])) {
        $promptperm = '<div><fb:prompt-permission perms="email,read_stream,publish_stream"><div style="width:97%; padding: 4px 8px; margin: 4px 0px; background-color:#ffebe8; border: 1px solid #dd3c10;">'.JText::_('Click here to allow us to create updates for your Facebook News Feed (e.g. when you upload a file)').'</div></fb:prompt-permission></div>';
        $this->assignRef ('promptperm', $promptperm);
        } 
      }
    }
  }
 
   
   function loadVflowModules()
   {
     global $vparams;
     jimport('joomla.application.module.helper');
      $vmods = array('vflow1', 'vflow2', 'vflow3', 'vflow4', 'vflow5', 'vflow6', 'vflow7', 'vflow8', 'vfshare', 'vflike');
      $pos = 1;
      foreach ($vmods as $vmods){
        $vmodule = &JModuleHelper::getModules($vmods);
        if (!empty($vmodule)){
        $vmodule = JModuleHelper::renderModule ($vmodule[0], array('style'=>'table'));
        } else {
        $vmodule = '';
        if ($vmods == 'vfshare') $vmodule = JText::_('Install any Joomla social bookmarking module and publish it to "vfshare" module position');
        }
        $vfpos = 'vflow'.$pos; 
        if ($vmods == 'vfshare') $vfpos = $vmods;  
        if ($vmods == 'vflike') $vfpos = $vmods;        
        if ($vparams->findvmods) $vmodule = '<h2>'.$vfpos.'</h2>';
        $this->assignRef ($vfpos, $vmodule);
        $pos++;
      }
   } 
   
   function setOwner()
   {
   global $vparams;
   $task = JRequest::getCmd ('task');  
   if ($task == 'myvids' || $task == 'mysubs'){
    $owner = JRequest::getVar ('juser');
    if (empty($owner)) $owner = & JFactory::getUser();
    if (!empty($owner->id) && $vparams->facebook) {
     include_once (JPATH_COMPONENT_SITE.DS.'helpers'.DS.'videoflow_user_manager.php');
     $fbowner =  VideoflowUserManager::getFBuserObj($owner->id);
    }
    } else if ($task == 'visit' || $task == 'subscribe'){
     $cid = JRequest::getInt('pid', JRequest::getInt('cid'));
     if (!empty($cid) && $vparams->facebook) {
     include_once (JPATH_COMPONENT_SITE.DS.'helpers'.DS.'videoflow_user_manager.php');
     $fbowner =  VideoflowUserManager::getFBuserObj($cid);
    }    
     $owner = & JFactory::getUser($cid);
    if (!$owner) return;
    if (!$owner->name) $owner->name = JText::_('Guest');
    } else {
    return;
    }
    $this->assignRef('owner', $owner);
    if (!empty($fbowner)) $this->assignRef('fbowner', $fbowner->fb_id);
    }
}